from BaseHTTPServer import BaseHTTPRequestHandler
import cgi,json
import DbClear,BankClear,AddMoney
import ConsistencyCheck

class HTTPRequestHandler(BaseHTTPRequestHandler):
    def do_POST(self):
        if self.path == '/api/test/clean_role':
            ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
            result = {
                "result": False,
                "info":""
            }
            if ctype == 'application/json':
                result = self.cleanStart(result)
                responseJson = json.dumps(result)
                self.createResponse(responseJson)
            else:
                result["info"] = "Missing json type"
                responseJson = json.dumps(result)
                self.createResponse(responseJson)
        elif self.path == '/api/test/clean_bankcard':
            ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
            result = {
                "result": False,
                "info": ""
            }
            if ctype == 'application/json':
                result = self.cleanBankcardStart(result)
                responseJson = json.dumps(result)
                self.createResponse(responseJson)
            else:
                result["info"] = "Missing json type"
                responseJson = json.dumps(result)
                self.createResponse(responseJson)
        elif self.path == '/api/test/add_money':
            ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
            result = {
                "result": False,
                "info": ""
            }
            if ctype == 'application/json':
                result = self.addMoney(result)
                responseJson = json.dumps(result)
                self.createResponse(responseJson)
            else:
                result["info"] = "Missing json type"
                responseJson = json.dumps(result)
                self.createResponse(responseJson)
        elif self.path == '/api/test/consistency_data':
            ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
            result = {
                "result": False,
                "info": ""
            }
            if ctype == 'application/json':
                result = self.consistencyStart(result)
                responseJson = json.dumps(result,default=lambda o: o.__dict__, sort_keys=False,indent=4)
                self.createResponse(responseJson)
            else:
                result["info"] = "Missing json type"
                responseJson = json.dumps(result)
                self.createResponse(responseJson)
        else:
            self.send_response(403)
            self.send_header('Content-Type', 'application/json')
            self.end_headers()


    def consistencyStart(self,result):
        length = int(self.headers.getheader('content-length'))
        data = self.rfile.read(length)
        mydata = json.loads(data)
        if "env" in mydata :
            env = mydata["env"]
            check = ConsistencyCheck.ConsistentData(env)
            checkResult = check.ConsistencyTask()
            if not checkResult:
                result["result"] = True
            else:
                result["info"] = checkResult
        else:
            result["info"] = "Missing env"
        return result


    def cleanStart(self,result):
        length = int(self.headers.getheader('content-length'))
        data = self.rfile.read(length)
        mydata = json.loads(data)
        if "env" in mydata and "uid" in mydata:
            env = mydata["env"]
            uid = mydata["uid"]
            token = mydata["token"]
            res = DbClear.DbClear(env, uid, token)
            cleanResult = res.dataClear()
            if cleanResult:
                result["result"] = cleanResult
            else:
                result["info"] = "Opteration Error"
        else:
            result["info"] = "Missing env or uid"
        return result


    def cleanBankcardStart(self,result):
        length = int(self.headers.getheader('content-length'))
        data = self.rfile.read(length)
        mydata = json.loads(data)
        if "env" in mydata and "bankcard" in mydata:
            env = mydata["env"]
            bankcard = mydata["bankcard"]
            res = BankClear.DbClear(env,bankcard)
            cleanResult = res.dataClear()
            if cleanResult:
                result["result"] = cleanResult
            else:
                result["info"] = "Opteration Error"
        else:
            result["info"] = "Missing env or bankcard"
        return result

    def addMoney(self,result):
        length = int(self.headers.getheader('content-length'))
        data = self.rfile.read(length)
        mydata = json.loads(data)
        if "env" in mydata and "uid" in mydata and "money" in mydata:
            env = mydata["env"]
            uid = mydata["uid"]
            money = mydata["money"]
            res = AddMoney.addMoney(env,uid,money)
            cleanResult = res.addmoney()
            if cleanResult:
                result["result"] = cleanResult
            else:
                result["info"] = "Opteration Error"
        else:
            result["info"] = "Missing env or uid or money"
        return result



    def createResponse(self, response):
        self.send_response(200)
        self.send_header('Content-Type', 'application/json')
        self.end_headers()
        self.wfile.write(response)
        self.wfile.close()
